package com.tbm.console.proj.service.impl;

import com.tbm.console.proj.entity.LocalLog;
import com.tbm.console.proj.entity.SystemButton;
import com.tbm.console.proj.repository.LocalLogRepository;
import com.tbm.console.proj.service.LocalLogService;
import com.tbm.console.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class LocalLogServiceImpl implements LocalLogService {

    @Autowired
    private LocalLogRepository localLogRepository;

    @Override
    public List<LocalLog> findAll() {
        return findAll(new PageData());
    }

    @Override
    public List<LocalLog> findAll(Integer start, Integer length) {
        return findAll(new PageData(),start,length);
    }

    @Override
    public List<LocalLog> findAll(PageData pageData) {
        Specification<LocalLog> sp = this.getLocalLogSpecification(pageData);
        return localLogRepository.findAll(sp);
    }

    @Override
    public List<LocalLog> findAll(PageData pageData, Integer start, Integer length) {
        Specification<LocalLog> sp = this.getLocalLogSpecification(pageData);
        Sort sort = new Sort(Sort.Direction.ASC,"id");
        Pageable pageable = new PageRequest(start, length, sort);
        return localLogRepository.findAll(sp,pageable).getContent();
    }

    @Override
    public long count() {
        return count(new PageData());
    }

    @Override
    public long count(LocalLog localLog) {
        return localLogRepository.count(Example.of(localLog));
    }

    @Override
    public long count(PageData pageData) {
        Specification<LocalLog> sp = this.getLocalLogSpecification(pageData);
        return localLogRepository.count(sp);
    }

    @Override
    public boolean deleteById(Integer id) {
        localLogRepository.deleteById(id);
        return false;
    }

    @Override
    public boolean deleteByIds(Iterable<LocalLog> iterable) {
        return false;
    }

    @Override
    public LocalLog save(LocalLog localLog) {
        List<LocalLog> list = saveAll(Arrays.asList(localLog));
        return list.size()==0?new LocalLog():list.get(0);
    }

    @Override
    public List<LocalLog> saveAll(List<LocalLog> localLogs) {
        return localLogRepository.saveAll(localLogs);
    }

    @Override
    public LocalLog findById(Integer id) {
        LocalLog localLog = new LocalLog();
        try{
            localLog = localLogRepository.findById(id).get();
        }catch (Exception e){

        }
        return localLog;
    }

    @Override
    public List<LocalLog> findAllById(Integer[] ids) {
        return localLogRepository.findAllById(Arrays.asList(ids));
    }

    //通过Specification来获取自定义的查询条件
    private Specification<LocalLog> getLocalLogSpecification(PageData pageData){
        return new Specification() {
            @Override
            public javax.persistence.criteria.Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> preList = new ArrayList<Predicate>();
                if (null != pageData.get("deleteFlag")) {
                    preList.add(criteriaBuilder.equal(root.get("deleteFlag"), pageData.get("deleteFlag").toString()));
                }
                if (null != pageData.get("system")) {
                    preList.add(criteriaBuilder.equal(root.get("logTitle"), pageData.get("system").toString()));
                }
                if (null != pageData.get("log")) {
                    preList.add(criteriaBuilder.equal(root.get("logType"), pageData.get("log").toString()));
                }
                if (null != pageData.get("uploadFlag")) {
                    preList.add(criteriaBuilder.equal(root.get("uploadFlag"), pageData.get("uploadFlag").toString()));
                }
//                if (null != pageData.get("logCommitterId")) {
//                    preList.add(criteriaBuilder.equal(root.get("logCommitterId"), pageData.get("logCommitterId").toString()));
//                }
                if (null != pageData.get("desc")) {
                    preList.add(criteriaBuilder.like(criteriaBuilder.upper(root.get("logDescription")), "%"+ pageData.get("desc").toString() + "%"));
                }
                Predicate[] p = new Predicate[preList.size()];
                return criteriaBuilder.and(preList.toArray(p));
            }
        };
    }
}
